# Credits:
# Bounty - For coming up with concept and original coding.
from es import createentity, entitysetvalue, fire, getplayerlocation, server
from superdice.superdice import registerRoll, unregisterRoll, players
from superdice.libs.playerExtras import getPlayer

def unload():
    unregisterRoll(diceRoll)

def round_end(ev):
    for userid in players:
        players[userid]['plant'] = False
        if players[userid]['planti']:
            fire(userid, players[userid]['planti'], 'Kill')
            players[userid]['planti'] = 0

def player_spawn(ev):
    userid = ev['userid']
    players[userid]['plant'] = False
    players[userid]['planti'] = 0

def player_death(ev):
    userid = ev['userid']
    players[userid]['plant'] = False
    index = players[userid]['planti']
    if index:
        fire(userid, index, 'Kill')
        players[userid]['planti'] = 0

def item_pickup(ev):
    userid = ev['userid']
    if not players[userid]['plant']:
        return

    getPlayer(userid).stripPlayer()

def diceRoll(userid, player, eplayer):
    eplayer.stripPlayer()
    player.model = 'props/cs_office/plant01.mdl'
    players[userid]['plant'] = True
    index = createentity('env_smokestack')
    entitysetvalue(index, 'targetname', index)
    entitysetvalue(index, 'origin', ' '.join(map(str, getplayerlocation(userid))))
    entitysetvalue(index, 'BaseSpread', 5)
    entitysetvalue(index, 'SpreedSpeed', 10)
    entitysetvalue(index, 'Speed', 80)
    entitysetvalue(index, 'StartSize', 8)
    entitysetvalue(index, 'EndSize', 16)
    entitysetvalue(index, 'rate', 40)
    entitysetvalue(index, 'JetLength', 200)
    entitysetvalue(index, 'rendercolor', '5 100 0')
    entitysetvalue(index, 'renderamt', 200)
    entitysetvalue(index, 'SmokeMaterial', 'effects/combinemuzzle2.vmt')
    server.queuecmd('es_xspawnentity ' + str(index))
    fire(userid, index, 'SetParent', '!activator')
    fire(userid, index, 'TurnOn')
    players[userid]['planti'] = index

registerRoll(diceRoll, 'plant')
